package com.smsc.headend.task.engine.handler.impl.firmware;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.smsc.headend.common.enums.DeviceType;
import com.smsc.headend.module.asset.entity.ComEntity;
import com.smsc.headend.module.data.enums.OperationGroup;
import com.smsc.headend.module.fwu.entity.LogFwUpgradeHis;
import com.smsc.headend.module.fwu.entity.TemplateFwUpgrade;
import com.smsc.headend.module.protocol.DeviceData;
import com.smsc.headend.module.task.dto.AtomicTask;
import com.smsc.headend.module.task.dto.Task;
import com.smsc.headend.task.engine.dao.TaskDAO;
import com.smsc.headend.task.engine.exception.UDISTaskHandlerException;
import com.smsc.headend.task.engine.handler.TaskHandler;
import com.smsc.headend.task.engine.service.FeignAssetManagementService;
import com.smsc.headend.task.engine.service.asset.AssetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


/**
 * @author xiaosc
 * @date 2021/3/4
 */
@Slf4j
public class UpgradeSettingsHandler implements TaskHandler {
    public static final String SUCCESS = "success";
    @Autowired
    TaskDAO taskDAO;
    @Autowired
    FeignAssetManagementService assetManagementService;
    @Autowired
    AssetService assetService;

    @Override
    public int handle(Task task, AtomicTask atomicTask, DeviceData cosemData, String actionResult) throws UDISTaskHandlerException {
        log.info("upgrade settings result: {}", actionResult);
        if(SUCCESS.equalsIgnoreCase(actionResult)) {
            updateFwuReport(task, atomicTask, LogFwUpgradeHis.UpgradeResult.success, SUCCESS);
            return 1;
        }

        updateFwuReport(task, atomicTask, LogFwUpgradeHis.UpgradeResult.failed, StrUtil.format("upgrade settings failed: {}", actionResult));
        return -1;
    }

    private void updateFwuReport(Task task, AtomicTask atomicTask, LogFwUpgradeHis.UpgradeResult result, String descr) {
        Map<String, Object> parameterMap = getParameterMap(task, atomicTask);
        Long taskId = MapUtil.getLong(parameterMap, "taskId");
        Long tvEnd = SystemClock.now() / 1000;
        if(atomicTask.getGroupId() == OperationGroup.DCUFtpSettings.getId().intValue()
                || atomicTask.getGroupId() == OperationGroup.MModUpgradeSettings.getId().intValue()) {
            ComEntity com = assetService.getComById(task.getComId());
            if(com != null) {
                assetManagementService.saveFwuReport(com.getDeviceId(), DeviceType.DCU.getId(), null, taskId, TemplateFwUpgrade.TaskType.FtpSettings.getCode(), result.getCode(), null, tvEnd, descr);
            }
        } else {
            String meterIdsStr = MapUtil.getStr(parameterMap, "meterIds");
            String[] meterIds = meterIdsStr.split(",");
            for (String meterId : meterIds) {
                assetManagementService.saveFwuReport(Convert.toLong(meterId), DeviceType.METER.getId(), null, taskId, TemplateFwUpgrade.TaskType.FtpSettings.getCode(), result.getCode(), null, tvEnd, descr);
            }
        }
    }

    @Override
    public int handleFailure(Task task, AtomicTask atomicTask, String message) {
        log.info("error {}, ", message);
        updateFwuReport(task, atomicTask, LogFwUpgradeHis.UpgradeResult.failed, StrUtil.format("upgrade settings failed: {}", message));
        return -1;
    }
}
